<template>
  <div>
    <BasicModal
      width="80%"
      v-bind="$attrs"
      @register="registerModal"
      :title="getTitle"
      @ok="handleSubmit"
    >
      <BasicForm @register="registerForm">
        <template #localSearch="{ model, field }">
          <ApiSelect
            :api="GettitlelistApi"
            showSearch
            :allowClear="true"
            v-model:value="model[field]"
            optionFilterProp="label"
            resultField="data"
            labelField="title"
            valueField="randomId"
          />
        </template>
      </BasicForm>
    </BasicModal>
  </div>
</template>
<script lang="ts">
  import { defineComponent, ref, computed, unref, watch } from 'vue';
  import { BasicForm, useForm, ApiSelect } from '/@/components/Form/index';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { AddApi, EditApi, GettitlelistApi } from '/@/api/advertising/index';
  import { FormSchema } from '/@/components/Table';
  import { genderTypes, ADTypes } from '/@/assets/dic';
  import { any } from 'vue-types';

  export const formSchema: FormSchema[] = [
    {
      label: 'id',
      field: 'id',
      component: 'Input',
      show: false,
    },
    {
      field: 'city',
      label: '所属城市:',
      component: 'Citydropdown',
      // required: true,
      colProps: { lg: 24, md: 24 },
    },
    // {
    //   field: 'type',
    //   label: '广告类别:',
    //   component: 'Select',
    //   componentProps: {
    //     options: ADTypes,
    //   },
    //   // required: true,
    //   defaultValue: ADTypes[0].value,
    // },
    {
      field: 'title',
      label: '标题:',
      component: 'Input',
      // required: true,
      slot: 'localSearch',
      colProps: { lg: 24, md: 24 },
    },
    {
      label: 'randomId',
      field: 'randomId',
      component: 'Input',
      show: false,
    },
    {
      label: 'imgUrl',
      field: 'imgUrl',
      component: 'Input',
      show: false,
    },
    {
      field: 'sort',
      label: '排序:',
      component: 'Input',
      // required: true,
      colProps: { lg: 24, md: 24 },
    },
  ];

  export default defineComponent({
    name: 'SalesUserModal',
    components: { BasicModal, BasicForm, ApiSelect },
    emits: ['success', 'register'],
    setup(_, { emit }) {
      const isUpdate = ref(true);
      const dataLsit = ref<any>([]);
      // 获取标题列表
      function getdataList() {
        GettitlelistApi({
          type: 0,
        }).then((res) => {
          dataLsit.value = res.data;
        });
      }
      getdataList();
      const [registerForm, { resetFields, setFieldsValue, validate }] = useForm({
        labelWidth: 160,
        schemas: formSchema,
        showActionButtonGroup: false,
        baseColProps: { lg: 12, md: 24 },
      });
      const [registerModal, { setModalProps, closeModal }] = useModalInner(async (data) => {
        resetFields();
        isUpdate.value = !!data?.isUpdate;

        if (unref(isUpdate)) {
          setFieldsValue({
            ...data.record,
          });
        }
      });

      const getTitle = computed(() => (!unref(isUpdate) ? '新增广告位' : '编辑广告位'));

      async function handleSubmit() {
        try {
          setModalProps({ confirmLoading: true });
          const values = await validate();
          dataLsit.value.map((item) => {
            if (item.randomId == values.title) {
              values.imgUrl = item.imgUrl;
              values.randomId = item.randomId;
              values.title = item.title;
              values.cityId = item.city;
            }
          });
          values.parentId = 0 | values.parentId;
          if (values.id > 0) {
            await EditApi(values);
          } else {
            await AddApi(values);
          }
          closeModal();
          emit('success');
        } catch (err) {
          console.log(err);
        } finally {
          setModalProps({ confirmLoading: false });
        }
      }

      return { registerModal, registerForm, getTitle, handleSubmit, GettitlelistApi };
    },
  });
</script>
